.TH "mcpwm.h" 3 "Wed Sep 16 2015" "Doxygen" \" -*- nroff -*-
.ad l
.nh
.SH NAME
mcpwm.h \- 
.SH SYNOPSIS
.br
.PP
\fC#include 'conf_general\&.h'\fP
.br
\fC#include 'datatypes\&.h'\fP
.br
\fC#include <stdbool\&.h>\fP
.br

.SS "Macros"

.in +1c
.ti -1c
.RI "#define \fBMCPWM_SWITCH_FREQUENCY_MIN\fP   3000"
.br
.ti -1c
.RI "#define \fBMCPWM_SWITCH_FREQUENCY_MAX\fP   40000"
.br
.ti -1c
.RI "#define \fBMCPWM_SWITCH_FREQUENCY_DC_MOTOR\fP   25000"
.br
.ti -1c
.RI "#define \fBMCPWM_DEAD_TIME_CYCLES\fP   100"
.br
.ti -1c
.RI "#define \fBMCPWM_RPM_TIMER_FREQ\fP   1000000\&.0"
.br
.ti -1c
.RI "#define \fBMCPWM_CMD_STOP_TIME\fP   0"
.br
.ti -1c
.RI "#define \fBMCPWM_DETECT_STOP_TIME\fP   500"
.br
.ti -1c
.RI "#define \fBMCPWM_PID_TIME_K\fP   0\&.001"
.br
.in -1c
.SS "Functions"

.in +1c
.ti -1c
.RI "void \fBmcpwm_init\fP (\fBmc_configuration\fP *configuration)"
.br
.ti -1c
.RI "const volatile \fBmc_configuration\fP * \fBmcpwm_get_configuration\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_set_configuration\fP (\fBmc_configuration\fP *configuration)"
.br
.ti -1c
.RI "void \fBmcpwm_init_hall_table\fP (int8_t *table)"
.br
.ti -1c
.RI "void \fBmcpwm_set_duty\fP (float dutyCycle)"
.br
.ti -1c
.RI "void \fBmcpwm_set_pid_speed\fP (float rpm)"
.br
.ti -1c
.RI "void \fBmcpwm_set_pid_pos\fP (float pos)"
.br
.ti -1c
.RI "void \fBmcpwm_set_current\fP (float current)"
.br
.ti -1c
.RI "void \fBmcpwm_set_brake_current\fP (float current)"
.br
.ti -1c
.RI "void \fBmcpwm_brake_now\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_release_motor\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_lock\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_unlock\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_lock_override_once\fP (void)"
.br
.ti -1c
.RI "int \fBmcpwm_get_comm_step\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_duty_cycle_set\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_duty_cycle_now\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_switching_frequency_now\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_rpm\fP (void)"
.br
.ti -1c
.RI "\fBmc_state\fP \fBmcpwm_get_state\fP (void)"
.br
.ti -1c
.RI "\fBmc_fault_code\fP \fBmcpwm_get_fault\fP (void)"
.br
.ti -1c
.RI "const char * \fBmcpwm_fault_to_string\fP (\fBmc_fault_code\fP fault)"
.br
.ti -1c
.RI "float \fBmcpwm_get_kv\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_kv_filtered\fP (void)"
.br
.ti -1c
.RI "int \fBmcpwm_get_tachometer_value\fP (bool reset)"
.br
.ti -1c
.RI "int \fBmcpwm_get_tachometer_abs_value\fP (bool reset)"
.br
.ti -1c
.RI "float \fBmcpwm_get_amp_hours\fP (bool reset)"
.br
.ti -1c
.RI "float \fBmcpwm_get_amp_hours_charged\fP (bool reset)"
.br
.ti -1c
.RI "float \fBmcpwm_get_watt_hours\fP (bool reset)"
.br
.ti -1c
.RI "float \fBmcpwm_get_watt_hours_charged\fP (bool reset)"
.br
.ti -1c
.RI "float \fBmcpwm_get_tot_current\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_tot_current_filtered\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_tot_current_directional\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_tot_current_directional_filtered\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_tot_current_in\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_tot_current_in_filtered\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_set_detect\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_detect_pos\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_reset_hall_detect_table\fP (void)"
.br
.ti -1c
.RI "int \fBmcpwm_get_hall_detect_result\fP (int8_t *table)"
.br
.ti -1c
.RI "int \fBmcpwm_read_hall_phase\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_read_reset_avg_motor_current\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_read_reset_avg_input_current\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_read_reset_avg_cycle_integrator\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_set_min_rpm\fP (float rpm)"
.br
.ti -1c
.RI "float \fBmcpwm_get_min_rpm\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_set_comm_mode\fP (\fBmc_comm_mode\fP mode)"
.br
.ti -1c
.RI "\fBmc_comm_mode\fP \fBmcpwm_get_comm_mode\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_last_adc_isr_duration\fP (void)"
.br
.ti -1c
.RI "float \fBmcpwm_get_last_inj_adc_isr_duration\fP (void)"
.br
.ti -1c
.RI "\fBmc_rpm_dep_struct\fP \fBmcpwm_get_rpm_dep\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_adc_inj_int_handler\fP (void)"
.br
.ti -1c
.RI "void \fBmcpwm_adc_int_handler\fP (void *p, uint32_t flags)"
.br
.in -1c
.SS "Variables"

.in +1c
.ti -1c
.RI "volatile uint16_t \fBADC_Value\fP []"
.br
.ti -1c
.RI "volatile int \fBADC_curr_norm_value\fP []"
.br
.ti -1c
.RI "volatile float \fBmcpwm_detect_currents\fP []"
.br
.ti -1c
.RI "volatile float \fBmcpwm_detect_voltages\fP []"
.br
.ti -1c
.RI "volatile float \fBmcpwm_detect_currents_diff\fP []"
.br
.ti -1c
.RI "volatile int \fBmcpwm_vzero\fP"
.br
.in -1c
.SH "Macro Definition Documentation"
.PP 
.SS "#define MCPWM_CMD_STOP_TIME   0"

.SS "#define MCPWM_DEAD_TIME_CYCLES   100"

.SS "#define MCPWM_DETECT_STOP_TIME   500"

.SS "#define MCPWM_PID_TIME_K   0\&.001"

.SS "#define MCPWM_RPM_TIMER_FREQ   1000000\&.0"

.SS "#define MCPWM_SWITCH_FREQUENCY_DC_MOTOR   25000"

.SS "#define MCPWM_SWITCH_FREQUENCY_MAX   40000"

.SS "#define MCPWM_SWITCH_FREQUENCY_MIN   3000"

.SH "Function Documentation"
.PP 
.SS "void mcpwm_adc_inj_int_handler (void)"

.SS "void mcpwm_adc_int_handler (void * p, uint32_t flags)"

.SS "void mcpwm_brake_now (void)"
Stop the motor and use braking\&. 
.SS "const char* mcpwm_fault_to_string (\fBmc_fault_code\fP fault)"

.SS "float mcpwm_get_amp_hours (bool reset)"
Get the amount of amp hours drawn from the input source\&.
.PP
\fBParameters:\fP
.RS 4
\fIreset\fP If true, the counter will be reset after this call\&.
.RE
.PP
\fBReturns:\fP
.RS 4
The amount of amp hours drawn\&. 
.RE
.PP

.SS "float mcpwm_get_amp_hours_charged (bool reset)"
Get the amount of amp hours fed back into the input source\&.
.PP
\fBParameters:\fP
.RS 4
\fIreset\fP If true, the counter will be reset after this call\&.
.RE
.PP
\fBReturns:\fP
.RS 4
The amount of amp hours fed back\&. 
.RE
.PP

.SS "\fBmc_comm_mode\fP mcpwm_get_comm_mode (void)"

.SS "int mcpwm_get_comm_step (void)"
Get the electrical position (or commutation step) of the motor\&.
.PP
\fBReturns:\fP
.RS 4
The current commutation step\&. Range [1 6] 
.RE
.PP

.SS "const volatile \fBmc_configuration\fP* mcpwm_get_configuration (void)"

.SS "float mcpwm_get_detect_pos (void)"

.SS "float mcpwm_get_duty_cycle_now (void)"

.SS "float mcpwm_get_duty_cycle_set (void)"

.SS "\fBmc_fault_code\fP mcpwm_get_fault (void)"

.SS "int mcpwm_get_hall_detect_result (int8_t * table)"
Get the current detected hall sensor table
.PP
\fBParameters:\fP
.RS 4
\fItable\fP Pointer to a table where the result should be stored
.RE
.PP
\fBReturns:\fP
.RS 4
0: OK -1: Invalid hall sensor output -2: WS2811 enabled -3: Encoder enabled 
.RE
.PP

.SS "float mcpwm_get_kv (void)"
Calculate the KV (RPM per volt) value for the motor\&. This function has to be used while the motor is moving\&. Note that the return value has to be divided by half the number of motor poles\&.
.PP
\fBReturns:\fP
.RS 4
The KV value\&. 
.RE
.PP

.SS "float mcpwm_get_kv_filtered (void)"
Calculate the FIR-filtered KV (RPM per volt) value for the motor\&. This function has to be used while the motor is moving\&. Note that the return value has to be divided by half the number of motor poles\&.
.PP
\fBReturns:\fP
.RS 4
The filtered KV value\&. 
.RE
.PP

.SS "float mcpwm_get_last_adc_isr_duration (void)"

.SS "float mcpwm_get_last_inj_adc_isr_duration (void)"

.SS "float mcpwm_get_min_rpm (void)"
Get the minimum allowed RPM in sensorless mode\&.
.PP
\fBReturns:\fP
.RS 4
The minimum allowed RPM\&. 
.RE
.PP

.SS "float mcpwm_get_rpm (void)"
Calculate the current RPM of the motor\&. This is a signed value and the sign depends on the direction the motor is rotating in\&. Note that this value has to be divided by half the number of motor poles\&.
.PP
\fBReturns:\fP
.RS 4
The RPM value\&. 
.RE
.PP

.SS "\fBmc_rpm_dep_struct\fP mcpwm_get_rpm_dep (void)"

.SS "\fBmc_state\fP mcpwm_get_state (void)"

.SS "float mcpwm_get_switching_frequency_now (void)"
Get the current switching frequency\&.
.PP
\fBReturns:\fP
.RS 4
The switching frequency in Hz\&. 
.RE
.PP

.SS "int mcpwm_get_tachometer_abs_value (bool reset)"
Read the absolute number of steps the motor has rotated\&.
.PP
\fBParameters:\fP
.RS 4
\fIreset\fP If true, the tachometer counter will be reset after this call\&.
.RE
.PP
\fBReturns:\fP
.RS 4
The tachometer value in motor steps\&. The number of motor revolutions will be this number divided by (3 * MOTOR_POLE_NUMBER)\&. 
.RE
.PP

.SS "int mcpwm_get_tachometer_value (bool reset)"
Read the number of steps the motor has rotated\&. This number is signed and will return a negative number when the motor is rotating backwards\&.
.PP
\fBParameters:\fP
.RS 4
\fIreset\fP If true, the tachometer counter will be reset after this call\&.
.RE
.PP
\fBReturns:\fP
.RS 4
The tachometer value in motor steps\&. The number of motor revolutions will be this number divided by (3 * MOTOR_POLE_NUMBER)\&. 
.RE
.PP

.SS "float mcpwm_get_tot_current (void)"
Get the motor current\&. The sign of this value will represent whether the motor is drawing (positive) or generating (negative) current\&.
.PP
\fBReturns:\fP
.RS 4
The motor current\&. 
.RE
.PP

.SS "float mcpwm_get_tot_current_directional (void)"
Get the motor current\&. The sign of this value represents the direction in which the motor generates torque\&.
.PP
\fBReturns:\fP
.RS 4
The motor current\&. 
.RE
.PP

.SS "float mcpwm_get_tot_current_directional_filtered (void)"
Get the filtered motor current\&. The sign of this value represents the direction in which the motor generates torque\&.
.PP
\fBReturns:\fP
.RS 4
The filtered motor current\&. 
.RE
.PP

.SS "float mcpwm_get_tot_current_filtered (void)"
Get the FIR-filtered motor current\&. The sign of this value will represent whether the motor is drawing (positive) or generating (negative) current\&.
.PP
\fBReturns:\fP
.RS 4
The filtered motor current\&. 
.RE
.PP

.SS "float mcpwm_get_tot_current_in (void)"
Get the input current to the motor controller\&.
.PP
\fBReturns:\fP
.RS 4
The input current\&. 
.RE
.PP

.SS "float mcpwm_get_tot_current_in_filtered (void)"
Get the FIR-filtered input current to the motor controller\&.
.PP
\fBReturns:\fP
.RS 4
The filtered input current\&. 
.RE
.PP

.SS "float mcpwm_get_watt_hours (bool reset)"
Get the amount of watt hours drawn from the input source\&.
.PP
\fBParameters:\fP
.RS 4
\fIreset\fP If true, the counter will be reset after this call\&.
.RE
.PP
\fBReturns:\fP
.RS 4
The amount of watt hours drawn\&. 
.RE
.PP

.SS "float mcpwm_get_watt_hours_charged (bool reset)"
Get the amount of watt hours fed back into the input source\&.
.PP
\fBParameters:\fP
.RS 4
\fIreset\fP If true, the counter will be reset after this call\&.
.RE
.PP
\fBReturns:\fP
.RS 4
The amount of watt hours fed back\&. 
.RE
.PP

.SS "void mcpwm_init (\fBmc_configuration\fP * configuration)"

.SS "void mcpwm_init_hall_table (int8_t * table)"
Initialize the hall sensor lookup table
.PP
\fBParameters:\fP
.RS 4
\fItable\fP The commutations corresponding to the hall sensor states in the forward direction- 
.RE
.PP

.SS "void mcpwm_lock (void)"
Lock the control by disabling all control commands\&. 
.SS "void mcpwm_lock_override_once (void)"
Allow just one motor control command in the locked state\&. 
.SS "int mcpwm_read_hall_phase (void)"
Read the current phase of the motor using hall effect sensors 
.PP
\fBReturns:\fP
.RS 4
The phase read\&. 
.RE
.PP

.SS "float mcpwm_read_reset_avg_cycle_integrator (void)"

.SS "float mcpwm_read_reset_avg_input_current (void)"

.SS "float mcpwm_read_reset_avg_motor_current (void)"

.SS "void mcpwm_release_motor (void)"
Disconnect the motor and let it turn freely\&. 
.SS "void mcpwm_reset_hall_detect_table (void)"
Reset the hall sensor detection table 
.SS "void mcpwm_set_brake_current (float current)"
Brake the motor with a desired current\&. Absolute values less than conf\&.cc_min_current will release the motor\&.
.PP
\fBParameters:\fP
.RS 4
\fIcurrent\fP The current to use\&. Positive and negative values give the same effect\&. 
.RE
.PP

.SS "void mcpwm_set_comm_mode (\fBmc_comm_mode\fP mode)"
Set the commutation mode for sensorless commutation\&.
.PP
\fBParameters:\fP
.RS 4
\fImode\fP COMM_MODE_INTEGRATE: More robust, but requires many parameters\&. COMM_MODE_DELAY: Like most hobby ESCs\&. Requires less parameters, but has worse startup and is less robust\&. 
.RE
.PP

.SS "void mcpwm_set_configuration (\fBmc_configuration\fP * configuration)"

.SS "void mcpwm_set_current (float current)"
Use current control and specify a goal current to use\&. The sign determines the direction of the torque\&. Absolute values less than conf\&.cc_min_current will release the motor\&.
.PP
\fBParameters:\fP
.RS 4
\fIcurrent\fP The current to use\&. 
.RE
.PP

.SS "void mcpwm_set_detect (void)"

.SS "void mcpwm_set_duty (float dutyCycle)"
Use duty cycle control\&. Absolute values less than MCPWM_MIN_DUTY_CYCLE will stop the motor\&.
.PP
\fBParameters:\fP
.RS 4
\fIdutyCycle\fP The duty cycle to use\&. 
.RE
.PP

.SS "void mcpwm_set_min_rpm (float rpm)"
Set the minimum allowed RPM in sensorless mode\&. This will affect startup performance\&. WARNING: Setting this too high can break stuff\&.
.PP
\fBParameters:\fP
.RS 4
\fIrpm\fP The minimum allowed RPM\&. 
.RE
.PP

.SS "void mcpwm_set_pid_pos (float pos)"
Use PID position control\&. Note that this only works when encoder support is enabled\&.
.PP
\fBParameters:\fP
.RS 4
\fIpos\fP The desired position of the motor in degrees\&. 
.RE
.PP

.SS "void mcpwm_set_pid_speed (float rpm)"
Use PID rpm control\&. Note that this value has to be multiplied by half of the number of motor poles\&.
.PP
\fBParameters:\fP
.RS 4
\fIrpm\fP The electrical RPM goal value to use\&. 
.RE
.PP

.SS "void mcpwm_unlock (void)"
Unlock all control commands\&. 
.SH "Variable Documentation"
.PP 
.SS "volatile int ADC_curr_norm_value[]"

.SS "volatile uint16_t ADC_Value[]"

.SS "volatile float mcpwm_detect_currents[]"

.SS "volatile float mcpwm_detect_currents_diff[]"

.SS "volatile float mcpwm_detect_voltages[]"

.SS "volatile int mcpwm_vzero"

.SH "Author"
.PP 
Generated automatically by Doxygen from the source code\&.
